package com.steven.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class SequenceStreamDemo {
	public static void main(String[] args) throws Exception {
		mergeMp3();
		
	}
	
	private static void mergeMp3(){
		List<FileInputStream> list = new ArrayList<FileInputStream>();
		
		try {
			list.add(new FileInputStream("c:/split/1.part"));
			list.add(new FileInputStream("c:/split/2.part"));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		
		final Iterator<FileInputStream> it = list.iterator();
		
		Enumeration<? extends InputStream> e = new Enumeration<FileInputStream>() {

			@Override
			public boolean hasMoreElements() {
				// TODO Auto-generated method stub
				return it.hasNext();
			}

			@Override
			public FileInputStream nextElement() {
				// TODO Auto-generated method stub
				return it.next();
			}
			
		};
		SequenceInputStream sis = new SequenceInputStream(e);
		
		BufferedInputStream bis = new BufferedInputStream(sis);
		BufferedOutputStream bos = null;
		try {
			 bos = new BufferedOutputStream(new FileOutputStream(new File("c:/split/test.mp3")));
		} catch (FileNotFoundException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		
		int len=0;
		
		try {
			while((len=bis.read())!=-1){
				bos.write(len);
				
				
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			bos.close();
			bis.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		
	}
private static void splitMp3(){
	
	try {
		BufferedOutputStream bos =null;
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("c:/1.mp3")));
		
		byte[] buf = new byte[1024*1024];
		 int count =1;
		 int len=0;
		try {
			while((len=bis.read(buf))!=-1){
				bos = new BufferedOutputStream(new FileOutputStream(new File("c:/split/"+count+".part")));
				bos.write(buf, 0, len);
				bos.close();
				count++;
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	
	 
	
	
	
}
	
	private static void merge() throws FileNotFoundException, IOException {
		Vector<InputStream> v = new Vector<InputStream>();
		v.add(new FileInputStream("aa.txt"));
		v.add(new FileInputStream("fos.txt"));
		v.add(new FileInputStream("log.txt"));
		
		v.add(new FileInputStream("fos.txt")); 
		 Enumeration<InputStream> e = v.elements();		
	 
		SequenceInputStream sis = new  SequenceInputStream(e);
		
		
		FileOutputStream fos = new FileOutputStream("sequencestreamdemo.txt");
		
		
		byte[] buf = new byte[1024];
		int len = 0;
		while((len=sis.read(buf))!=-1){
			fos.write(buf, 0, len);
			
		}
		
		sis.close();
		fos.close();
	}

}
